const SERVICE = require("../../lib/js/service.js");

Page({
  data: {
    word: 0,
    showQueryNavItem: 1,    //显示查询导航项
    // vinDiscren: null,    //VIN识别
    showBg: false,
    platenumbers: "",
    date: null,  //日期
    showPlateNumbers: false,
    shengVal: null,
    showHintInfo: false,    //提示
    imgSrc: SERVICE.imgUrl,
    agreement: {
      show: false,
      quit: "AgreementQuit",
      consent: "AgreementConsent"
    },
    photoData: {
      show: false
    }
  },

  onLoad() {
    this.setData({
      date: new Date()
    });

  },

  /*分享*/
  onShareAppMessage() {

  },

  /*查询*/
  Query(e) {
    this.setData({
      showQueryNavItem: e.currentTarget.dataset.index,
      showPlateNumbers: false,
      showHintInfo: false,
      word: 0
    });
  },

  /*照相*/
  photo() {
    let _this = this;
    this.setData({
      showBg: true
    });
  },

  photoType(e) {
    let _this = this, td = this.data,
      tapIndex = e.currentTarget.dataset.index, SType = ['album', 'camera'];
    wx.chooseImage({
      count: 1,  // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: [SType[tapIndex]], // 可以指定来源是相册还是相机，默认二者都有
      success: function (imgRes) {
        wx.showLoading({
          title: "识别中...",
          mask: true
        });
        wx.uploadFile({
          url: "https://www.csdashi.cn/apiservice/parseDrivingLicense/",
          filePath: imgRes.tempFilePaths[0],
          name: "image",
          success: function (res) {
            console.log("图片：", res);
            wx.hideLoading();

            td.agreement.show = true;
            let pay_info = {
              Vin: JSON.parse(res.data).vin,
              CX: "--",
              PP: "--"
            };
            _this.setData({
              agreement: td.agreement,
              photoData: {
                show: true,
                queryImg: imgRes.tempFilePaths[0],
                pay_info: JSON.stringify(pay_info)
              },
              showBg: false
            });

            // let vinDiscren = JSON.parse(res.data);
            // if (vinDiscren.status == "success") {
            //   _this.setData({
            //     platenumbers: vinDiscren.vin,
            //     showBg: false
            //   });
            //   wx.showLoading({
            //     title: "VIN码检验中...",
            //     mask: true
            //   });

            //   SERVICE.POST(SERVICE.URL.getVinGetVehicleModel, {
            //     vin: vinDiscren.vin
            //   }, (status, res) => {
            //     wx.hideLoading();
            //     if (res.data.status === "success") {
            //       let query = res.data.result;
            //       td.agreement.show = true;
            //       _this.setData({
            //         photoData: {
            //           show: true,
            //           queryImg: imgRes.tempFilePaths[0],
            //           pay_info: JSON.stringify(query),
            //         },
            //         agreement: td.agreement,
            //       });
            
            //     } else {
            //       _this.setData({
            //         showHintInfo: true
            //       });
            //     }
            //   });
            // } else {
            //   wx.showToast({
            //     title: "识别失败",
            //     icon: "none",
            //     mask: true
            //   });
            // }
          }
        });
      },
    })

  },

  cancelPhtoto() {
    this.setData({
      showBg: false
    });
  },

  /*日期*/
  bindDateChange(e) {
    this.setData({
      dateVal: e.detail.value
    });
  },

  /*车况报告*/
  query(e) {
    let _this = this, td = this.data, form_obj = e.detail.value;
    if (!form_obj.vin) {
      wx.showToast({
        title: "请输入VIN号",
        icon: "none",
        mask: true
      });
    } else if (form_obj.vin.length < 17) {
      wx.showToast({
        title: "请输入完整的VIN号",
        icon: "none",
        mask: true
      });
    } else {
      wx.showLoading({
        title: "VIN码检验中...",
        mask: true
      });
      SERVICE.POST(SERVICE.URL.getVinGetVehicleModel, {
        vin: form_obj.vin
      }, (status, res) => {
        wx.hideLoading();
        console.log("获取车型：", res);
        if (res.data.status === "success" || res.data.status === "other") {
          td.agreement.show = true;
          _this.setData({
            showHintInfo: false,
            agreement: td.agreement,
            pay_info: res.data.result
          });
        } else {
          _this.setData({
            showHintInfo: true
          });
        }
      });
    }
  },

  /*出险报告*/
  historyQuery(e) {
    let _this = this, td = this.data, form_obj = e.detail.value;
    if (!form_obj.vin) {
      wx.showToast({
        title: "请输入VIN号",
        icon: "none",
        mask: true
      });
    } else if (form_obj.vin.length < 17) {
      if (!form_obj.vin) {
        wx.showToast({
          title: "请输入完整的VIN号",
          icon: "none",
          mask: true
        });
      }
    } else {
      td.agreement.show = true;
      _this.setData({
        agreement: td.agreement,
        historyQuery_form: form_obj
      });
    }
  },

  
  /*车型解析*/
  modelQueries(e) {
    let _this = this, td = this.data, form_obj = e.detail.value;
    if (!form_obj.vin) {
      wx.showToast({
        title: "请输入VIN号",
        icon: "none",
        mask: true
      });
    } else if (form_obj.vin.length < 17) {
      wx.showToast({
        title: "请输入完整的VIN号",
        icon: "none",
        mask: true
      });
    } else {
      td.agreement.show = true;
      _this.setData({
        agreement: td.agreement,
        modelQueries_vin: form_obj.vin
      });

    }
  },

  /*省份*/
  province() {
    this.setData({
      showPlateNumbers: true
    });
  },
  sheng(e) {
    this.setData({
      shengVal: e.detail.val,
      showPlateNumbers: false
    });
  },
  inputQuery(e) {
    let td = this.data, val = e.detail.value;
    let val_2 = "";
    for (let i of val) {
      if (!/[\u4e00-\u9fa5]/.test(i)) {
        val_2 += i;
      }
    }
    this.setData({
      inputQuery_val: val_2.toUpperCase(),
      word: val.length,
      showHintInfo: false
    });
  },

  inputhistoryQuery(e) {
    let td = this.data, val = e.detail.value;
    if (val.length > 0) {
      let val_2 = "";
      for (let i of val) {
        if (!/[\u4e00-\u9fa5]/.test(i)) {
          val_2 += i;
        }
      }
      this.setData({
        historyQuery_val: val_2.toUpperCase(),
        word: val.length,
        showHintInfo: false
      });
    } else {
      this.setData({
        word: 0,
        showHintInfo: false
      });
    }
  },

  model(e) {
    let td = this.data, val = e.detail.value;
    if (val.length > 0) {
      let val_2 = "";
      for (let i of val) {
        if (!/[\u4e00-\u9fa5]/.test(i)) {
          val_2 += i;
        }
      }
      this.setData({
        inputModelQueries_val: val_2.toUpperCase(),
        word: val.length,
        showHintInfo: false
      });
    } else {
      this.setData({
        word: 0,
        showHintInfo: false
      });
    }
  },

  vehicleModel() {
    wx.showToast({
      title: "功能即将上线",
      icon: "none",
      mask: true
    });
  },
  charge2() {
    wx.navigateTo({
      url: "../charge2/charge2",
    });
  },

  AgreementQuit() {
    let td = this.data;
    td.agreement.show = false;
    this.setData({
      agreement: td.agreement
    });
  },
  AgreementConsent() {
    let _this = this, td = this.data;
    if (td.photoData.show == false) {
      if (td.showQueryNavItem == 1) {
        wx.navigateTo({
          url: "../order-pay/order-pay?pay_info=" + JSON.stringify(td.pay_info) + "&types=" + (td.showQueryNavItem - 1),
          success() {
            td.agreement.show = false;
            _this.setData({
              agreement: td.agreement
            });
          }
        })
      } else if (td.showQueryNavItem == 2) {
        td.agreement.show = false;
        _this.setData({
          agreement: td.agreement
        });
        wx.showLoading({
          title: "VIN码检验中...",
          mask: true
        });
        SERVICE.POST(SERVICE.URL.getVinGetVehicleModel, {
          vin: td.historyQuery_form.vin
        }, (status, res) => {
          wx.hideLoading();
          if (res.data.status === "success") {
            let query = res.data.result;
            wx.navigateTo({
              url: "../order-pay/order-pay?pay_info=" + JSON.stringify(query) + "&types=" + (td.showQueryNavItem - 1),
              success() {
                showHintInfo: false,
                  td.agreement.show = false;
                _this.setData({
                  agreement: td.agreement
                });
              }
            });
          } else {
            _this.setData({
              showHintInfo: true
            });
          }
        });
      } else if (td.showQueryNavItem == 3) {
        td.agreement.show = false;
        _this.setData({
          agreement: td.agreement
        });
        wx.showLoading({
          title: "VIN码检验中...",
          mask: true
        });
        SERVICE.POST(SERVICE.URL.getVinGetVehicleModel, {
          vin: td.modelQueries_vin
        }, (status, res) => {
          wx.hideLoading();
          if (res.data.status === "success") {
            let query = res.data.result;
            _this.setData({
              showHintInfo: false
            });
            wx.navigateTo({
              url: "../model-queries-data/model-queries-data?model_queries_data=" + JSON.stringify(query),
            });
          } else {
            _this.setData({
              showHintInfo: true
            });
          }
        });

      }
    } else {
      wx.navigateTo({
        url: "../order-pay/order-pay?pay_info=" + td.photoData.pay_info +
          "&types=" + (td.showQueryNavItem - 1) + "&queryImg=" + td.photoData.queryImg,
        success() {
          showHintInfo: false,
          td.agreement.show = false;
          td.photoData.show = false;
          _this.setData({
            agreement: td.agreement,
            photoData: td.photoData
          });
        }
      });
    }

  },
  clear() {
    let td = this.data;
    this.setData({
      word: 0,
      inputQuery_val: "",
      historyQuery_val: "",
      inputModelQueries_val: "",
      showHintInfo: false
    });
  }
});